package smpptester;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author motaz
 */
import java.io.IOException;
import java.util.Date;

import org.jsmpp.bean.BindType;
import org.jsmpp.bean.ESMClass;
import org.jsmpp.bean.GeneralDataCoding;
import org.jsmpp.bean.NumberingPlanIndicator;
import org.jsmpp.bean.RegisteredDelivery;
import org.jsmpp.bean.SMSCDeliveryReceipt;
import org.jsmpp.bean.TypeOfNumber;
import org.jsmpp.session.BindParameter;
import org.jsmpp.session.SMPPSession;
import org.jsmpp.util.AbsoluteTimeFormatter;
import org.jsmpp.util.TimeFormatter;

public class SMPPClient {

    SMPPSession smppSession = null;
    boolean isBound = false;
    MessageReceiverListenerImpl rc = new MessageReceiverListenerImpl();
    String fsmppServer;
    int fsmppPort;
    String fsmppUser;
    String fsmppPassword;
    boolean isRX;
    boolean isTX;

    public SMPPClient(String smppServer, int smppPort, String smppUser, String smppPassword,
            boolean isrx, boolean istx) {
        super();
        fsmppServer = smppServer;
        fsmppPort = smppPort;
        fsmppUser = smppUser;
        fsmppPassword = smppPassword;
        isRX = isrx;
        isTX = istx;
    }

    @Override
    protected void finalize() throws Throwable {
        if (isBound) {
            unbind();
        }
        super.finalize();
    }

    public void init(String smppServer, int smppPort, String smppUser, String smppPassword,
            boolean isrx, boolean istx) {
        fsmppServer = smppServer;
        fsmppPort = smppPort;
        fsmppUser = smppUser;
        fsmppPassword = smppPassword;
        isRX = isrx;
        isTX = istx;
    }

    public ResultRec bind() {
        ResultRec result = new ResultRec();
        try {
            BindType trx;
            if (isRX && isTX) {
                trx = BindType.BIND_TRX;
            } else if (isRX) {
                trx = BindType.BIND_RX;
            } else if (isTX) {
                trx = BindType.BIND_TX;
            } else {
                trx = null;
            }

            // bind param instance is created with parameters for binding with SMSC
            BindParameter bP = new BindParameter(
                    trx,
                    fsmppUser,
                    fsmppPassword,
                    "any",
                    TypeOfNumber.INTERNATIONAL,
                    NumberingPlanIndicator.UNKNOWN,
                    null);
            // smpp session is created using the bindparam and the smsc ip address/port
            smppSession = new SMPPSession();
            smppSession.connectAndBind(fsmppServer, fsmppPort, bP);
            isBound = true;
            result.resultCode = 0;
            result.resultMsg = "Connected successfully\n";
            return (result);

        } catch (IOException ex) {
            isBound = false;
            result.resultCode = 1;
            result.resultMsg = "Error while connecting to : " + fsmppServer + "\n "
                    + ex.toString() + "\n";
            return (result);
        }
    }

    public ResultRec sendTextMessage(String fromMDN, String toMDN, String shortMessage) {
        ResultRec res = new ResultRec();



        GeneralDataCoding dataCoding = new GeneralDataCoding(8);
        //false, true,
        //MessageClass.CLASS1, Alphabet.ALPHA_UCS2);

        ESMClass esmClass = new ESMClass();
        TimeFormatter tF = new AbsoluteTimeFormatter();

        try {
            // submitShortMessage(..) method is parametrized with necessary
            // elements of SMPP submit_sm PDU to send a short message
            // the message length for short message is 140
            String msgid = smppSession.submitShortMessage(
                    "ESME",
                    TypeOfNumber.NATIONAL,
                    NumberingPlanIndicator.ISDN,
                    fromMDN,
                    TypeOfNumber.NATIONAL,
                    NumberingPlanIndicator.ISDN,
                    toMDN,
                    esmClass,
                    (byte) 0,
                    (byte) 0,
                    tF.format(new Date()),
                    null,
                    new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT),
                    (byte) 0,
                    dataCoding,
                    (byte) 0,
                    shortMessage.getBytes("UTF-16BE"));
            res.resultCode = 0;
            res.resultMsg = msgid;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            res.resultCode = 1;
            res.resultMsg = ex.toString();

        }
        return (res);

    }

    public String unbind() {
        try {
            smppSession.unbindAndClose();
            isBound = false;
            return ("Unbound successfully\n");
        } catch (Exception ex) {
            isBound = false;
            return ("Error while unbinding : \n" + ex.toString() + "\n");
        }

    }

    public String receive() {
        smppSession.setMessageReceiverListener(rc);
        System.out.println("Receiving messages..");
        return ("Reception started\n");

    }

    public boolean checkIsBound() {
        return (isBound);
    }
}
